'use strict';


// 查询趣事分享列表
const db = uniCloud.database();
exports.main = async (event, context) => {

	// 查询条件
	let parmse = {};
	
	let res = null;

	if (event.state == 'add') {
		parmse = event
		delete parmse['state'] //删除这个多余的状态
		res = await db.collection('planList').add(parmse);
		return {
			code: 200,
			message: "操作成功"
		}
	} else if (event.state == 'update') {
		parmse = event
		let _id = event._id
		delete parmse['state'] //删除这个多余的状态
		delete parmse['_id'] //删除这个多余的状态
		res = await db.collection('planList').where({
			_id: _id
		}).update(parmse)
		
		return {
			code: 200,
			message: "操作成功"
		}
	} else if (event.state == 'look') {
		parmse = event
		delete parmse['state']
		
		if(parmse.planName){
			parmse.planName = new RegExp(parmse.planName, 'g')
		}else{
			delete parmse['planName']
		}
		
		res = await db.collection('planList').where(db.command.or({
			...parmse
		})).orderBy('createTime', 'desc').get() //获取数据
		
		return {
			code: 200,
			message: "操作成功",
			data: res.data,
			total:res.affectedDocs
		}
	}else if(event.state == 'delete'){
		parmse = event
		
		delete parmse['state']
		
		console.log(parmse._id , "parmse._id");
		
		let res = await db.collection('planList').doc(parmse._id).remove()
		
		return {
			code: 200,
			message: "操作成功"
		}
	}

}